package com.ozo.cloud.common.base.constant;

/**
 * 枚举基类
 *
 * @author qiangesoft
 * @date 2024-11-19
 */
public interface BaseEnum {

    String getCode();

    String getDesc();

    /**
     * 获取实例
     *
     * @param enumClass
     * @param code
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> E getInstance(Class<E> enumClass, String code) {
        for (E e : enumClass.getEnumConstants()) {
            if (e.getCode().equals(code)) {
                return e;
            }
        }
        throw new IllegalArgumentException("No enum constant " + enumClass.getCanonicalName() + " with code " + code);
    }

    /**
     * 获取实例描述
     *
     * @param code
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> String getInstanceDesc(Class<E> clazz, String code) {
        return getInstance(clazz, code).getDesc();
    }

    /**
     * 校验code是否合法
     *
     * @param code
     * @return
     * @param <E>
     */
    static <E extends BaseEnum> Boolean isValid(Class<E> enumClass, String code) {
        for (E e : enumClass.getEnumConstants()) {
            if (e.getCode().equals(code)) {
                return true;
            }
        }
        return false;
    }

}